package com.lhy.mymall.config;

import com.lhy.mymall.flter.UserFilter;
import com.lhy.mymall.flter.UserJWTFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @author: 卢辉宇
 * @date: 2022/12/14 22:50
 * @description: 用户过滤器配置类
 */
@Configuration
public class UserJWTFilterConfig {

    /**
     * 通过spring的bean创建需要配置的AdminFilter过滤类
     * @return 需要配置的过滤类
     */
    @Bean
    public UserJWTFilter userJWTFilter(){
        return new UserJWTFilter();
    }

    /**
     * 注册上面的得到的AdminFilter，说明要过滤的url
     * @return 过滤器注册对象
     */
    @Bean(name = "UserJWTFilterConf")
    public FilterRegistrationBean UserJWTFilterConfig(){
        //通过FilterRegistrationBean来对象，注册自定义过滤器
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();

        //注册AdminFilter自定义过滤器
        filterFilterRegistrationBean.setFilter(userJWTFilter());

        //自定义过滤器要拦截的url(即需要登录校验的请求)
        filterFilterRegistrationBean.addUrlPatterns("/cart/*");
        filterFilterRegistrationBean.addUrlPatterns("/order/*");
        filterFilterRegistrationBean.addUrlPatterns("/user/update");

        //给过滤器命个名,以便区分是那个过滤器
        filterFilterRegistrationBean.setName("UserJWTFilterConf");
        return filterFilterRegistrationBean;
    }

}

